<?php
class Article_Type_Model extends CI_Model
{
    public $name;
    public $pid;


    function __construct()
    {
        $this->setTableName("article_type");
        parent::__construct();
    }

    /**
     * 搜索方法
     */
    public function search()
    {
        $this->setQuery($this->db->get($this->getTableName()));
        return $this->mappingList();
    }

    /**
     * 查询pid 为0 分类
     */
    public function listParents()
    {
        return $this->loadByPid(0);
    }

    public function loadByPid($pid)
    {
        $this->db->where("pid", $pid);
        $this->db->from($this->getTableName());
        $this->setQuery($this->db->get());
        return $this->getQuery()->result();
    }

    public function getSqlIds($pid)
    {
        $ids_array = array();
        array_push($ids_array, $pid);
        foreach ($this->loadByPid($pid) as $key => $value) {
            array_push($ids_array, $value->id);
        }
        return $ids_array;
    }

    public
    function setName($name)
    {
        $this->name = $name;
    }

    public
    function getName()
    {
        return $this->name;
    }

    public
    function setPid($pid)
    {
        $this->pid = $pid;
    }

    public
    function getPid()
    {
        return $this->pid;
    }

    public
    function getParentName()
    {
        if ($this->getPid() == 0) return "无";
        $typeM = clone $this;
        $typeM->setId($this->getPid());
        return $typeM->load()->name;
    }


}